#pragma once
namespace lzf_list
{
	template<class T>
	class list_node
	{
		list_node(const T& It)
			:_prev(nullptr),
			_next(nullptr),
			_data(It)
		{
		}
	private:
		T* _prev;
		T* _next;
		T _data;
	};
	template<class T,class Ref,class Ptr>
	class list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T> iterator;

		list_iterator(node *p)
			:_pnode(p)
		{}
		
		Ptr operate->()
		{
			return &_pnode._data;
		}
		
		Ref operator* ()
		{
			return _pnode->_data;
		}

		iterator& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		iterate operator++(int)
		{
			iterator it(*this);
			_pnode = _pnode->_next;
			return it;
		}

		iterator& operator--()
		{
			_pnode = _pnode->_prev;
			return *this
		}

		iterator operator--(int)
		{
			iterator it(*this);
			_pnode = _pnode->_prev;
			return it;
		}

		bool operator!=(const iterator &it)
		{
			return _pnode != it._node;
		}

		bool operator!=(const iterator& it)
		{
			return _pnode == it._node;
		}

	private:
		node* _pnode;
	};

	template<class T>
	class list
	{
		typedef list_node<T> node;
		typedef list_iterator<T, T*, T&> iterator;
		typedef list_iterator<T,const T*,const T&> const_iterator;
		
		const_iterator begin()const
		{
			return iterator(_head->_next);
		}

		const_iterator end()const
		{
			return iterator(_head);
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		void empty_initialize()
		{
			_head = new node(T());
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_initialize();
		}

		template <class Inputerator>
		list(Inputerator first, Inputerator end)
		{
			empty_initialize();
			while (first != end)
			{
				push_back(*first);
				first++;
			}
		}

		void swap(list<T>& It)
		{
			std::swap(_head, it._head);
		}

		list(const list<T>& It)
		{
			empty_initialize();
			list<T> tmp(It.begin(), It.end());
			swap(tmp);
		}

		list<T>& operator=(list<T> It)
		{
			swap(It);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		void push_back(const T& x)
		{
			//node* newnode = new node(x);
			//node* tail = _head->_prev;
			//// _head         tail   newnode
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;

			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

		iterator insert(iterator pos, const T& x)
		{
			node* newnode = new node(x);
			node* cur = pos._pnode;
			node* prev = cur->_prev;

			// prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;

			++_size;

			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			node* prev = pos._pnode->_prev;
			node* next = pos._pnode->_next;

			prev->_next = next;
			next->_prev = prev;

			delete pos._pnode;
			--_size;

			return iterator(next);
		}

		void push_back(const T& It)
		{
			node* newnode = new node(It);
			node* tail = _head._prev;
			node* prev = tail->_prev;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = tail;
			tail->prev = newnode;
		};







	private:
		node* _head;
	};








}